/*
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-03-22 12:12:49
 * @LastEditTime: 2024-07-11 17:52:50
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description: 拖拽设计模块
 * @FilePath: \chu-meng\client\src\store\modules\DragDesign.js
 * @Copyright 版权声明
 */


const app = {
  state: {
    // 当前拖拽组件
    currentDragComponent: {
      // 组件id
      id: '',
      // 组件名称
      name: '',
      // 组件类型
      type: '',
      // 组件配置组件类型
      configType: '',
      // 组件配置
      config: {}
    },
    // 组件集合
    componentList: [],
    // 当前选中的组件
    currentSelectedComponentInfo: {
      indexPath: [],
      index: '',
      id: '',
      type: '',
      configType: '',
      config: {}
    }
  },
  getters: {
    currentDragComponent (state) {
      return state.currentDragComponent
    },
    componentList (state) {
      return state.componentList
    },
    currentSelectedComponentInfo (state) {
      return state.currentSelectedComponentInfo
    }
  },
  mutations: {
    SET_CURRENT_DRAG_COMPONENT: (state, data) => {
      state.currentDragComponent = data
    }
  },
  actions: {
    // 设置当前拖拽的组件
    SetCurrentDragComponent: ({ commit }, data) => {
      commit('SET_CURRENT_DRAG_COMPONENT', data)
    },
    // 增加一个组件
    AddComponent: ({ commit, state }) => {
      state.componentList.push(state.currentDragComponent)
    },
    // 设置当前选中的组件
    SetCurrentSelectedComponent: ({ commit, state }, data) => {
      console.log('====+++', data)
      state.componentList.forEach((item, index) => {
        if (item.id === data.id) {
          state.currentSelectedComponentInfo = {
            indexPath: [index],
            index,
            id: data.id,
            type: data.type,
            configType: data.configType,
            config: data.config
          }
        }
      })
      console.log(`🚀 ~ state.componentList.forEach ~ state.currentSelectedComponentInfo:`, state.currentSelectedComponentInfo)
    }
  }
}

export default app
